package cn.edu.scau.cmi.wangmaoren.client.hibenate;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Transaction;
import cn.edu.scau.cmi.wangmaoren.hibernate.dao.MajorDAO;
import cn.edu.scau.cmi.wangmaoren.hibernate.domain.Major;

public class HibernateClient6HibernateDao
{

	public static void main(String[] args)
	{
		Major major = new Major();
		major.setName("服装工程");
		major.setId(6L);

		newMajor(major);
		listMajors();
	}

	public static void newMajor(Major major)
	{
		MajorDAO majorDAO = new MajorDAO();
		// 要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
		Transaction transaction = majorDAO.getSession().beginTransaction();
		majorDAO.save(major);
		transaction.commit();
		// (3) 或者不适用事务，使用flush从缓冲区中将数据持久化到数据库中
		// majorDAO.getSession().flush();
	}

	private static void listMajors()
	{
		MajorDAO MajorDAO = new MajorDAO();
		List Majors = MajorDAO.findAll();
		Iterator<?> MajorIterator = Majors.iterator();
		while (MajorIterator.hasNext())
		{
			Major Major = (Major) MajorIterator.next();
			System.out.println("调用DAO后的专业名是：" + Major.getName() + "，调用DAO后的专业ID号是：" + Major.getId());
		}
	}
}